//
//  DataHandle.m
//  BTRIP
//
//  Created by Rain on 15/11/12.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "DataHandle.h"
#import <AFNetworking.h>
@implementation DataHandle

+ (void)contectWithURL:(NSString *)url andParameters:(NSDictionary *)parameters andViewController:(UIViewController *)viewController andBlock:(ContectionBlock)block
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    if (parameters == nil) {
        [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            
            NSLog(@"网络请求成功");
            block (responseObject);
            
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            NSLog(@"哪里错了啊：%@", error);
            
            //提示框
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接失败" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:alertAction];
            [viewController presentViewController:alertController animated:YES completion:nil];
        }];
    }else{
        
        [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSLog(@"网络请求成功");
            block(responseObject);
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
        
    }
    
}


+ (void) getUrl:(NSString *)url
           body:(id)body
       response:(ResposeStyle)style
requestHeadFile:(NSDictionary *)headFile
 viewController:(UIViewController *)viewController
        success:(void (^)(NSURLSessionDataTask *task, id resposeObject))success
        failure:(void (^)(NSURLSessionDataTask *task, NSError *error)) failure {
    
    // 1. 获取单例的网络管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2. 根据style的类型，去选择返回值的类型
    switch (style) {
        case JSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case Data:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 3.设置响应数据支持类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript", nil]];
    
    // 4.给网络请求添加HTTP协议头文件
    if (headFile) {
        for (NSString *key in headFile) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    //    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    //    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    //    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    
    // 5. UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 6. 发送get请求
    [manager GET:url parameters:body success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(task, error);
        }
        
        //提示框
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接失败" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:alertAction];
        [viewController presentViewController:alertController animated:YES completion:nil];
        
    }];
    
}


@end
